import { Component, OnInit, ViewChild, ViewContainerRef, Renderer2 } from '@angular/core';
import { HttpHelper } from '../../../Helper/HttpHelper';
import { SmartTableDataSource } from '../../../Helper/SmartTableDataSource';
import { Fun } from '../../../Config/Fun';
import { DtoResultObj } from '../../../Model/DtoRec/DtoResult';
import { NbWindowService } from '@nebular/theme';
import { ServerSourceConf } from '../../../Lib/ng2-smart-table/lib/lib/data-source/server/server-source.conf';

@Component({
  selector: 'wzl-course-edit',
  templateUrl: './course-edit.html',
})
export class EditComponent implements OnInit {
  @ViewChild('samrtTable', { static: true, read: ViewContainerRef }) container: ViewContainerRef;
  @ViewChild('btnHead', { static: true }) template;
  source: SmartTableDataSource;
  /** 静态方法，获取默认配置 */
  settings: any = SmartTableDataSource.getDefaultSetting();
  configJson: any = {}
  /** 显示的列数 */
  clmNum: number = 0

  checkedList = [{
    classesName: "",
    weekNum: "",
    obj: null,
  }]
  constructor(
    private HttpHelper: HttpHelper,
    private windowService: NbWindowService,
    public httpHelper: HttpHelper,
    private renderer: Renderer2
  ) {
    this.checkedList = [];
    let smartTableCofnig: ServerSourceConf = new ServerSourceConf();
    smartTableCofnig.endPoint = "WeekCourse/CourseTable";
    smartTableCofnig.pagerLimitKey = ""
    this.source = new SmartTableDataSource(this.HttpHelper, smartTableCofnig, "query");
    this.configJson = {
      "班级": {
        "title": '班级'
      }
    }

    for (let week = 1; week <= 5; week++) {
      for (let num = 1; num <= 7; num++) {
        this.configJson[week + "-" + num] = {
          "title": week + "-" + num,
          "type": "checkbox",
        };
      }
    }

    this.settings.columns = this.configJson;
    console.log(this.settings.columns)
    this.settings.actions["add"] = true;
    this.settings.pager["perPage"] = 10;
    this.source.setting = this.settings;

  }

  ngOnInit() {

  }

  async onSave(event): Promise<void> {

  }

  /**
   * 
   * @param event 添加事件
   */
  onDelete(event): void {

  }

  cellSelect(obj) {
    console.log(obj);
    let thisObj = {
      classesName: obj.cell.row.data.班级,
      weekNum: obj.obj.value,
      obj: obj.obj,
    }

    for (let index = 0; index < this.checkedList.length; index++) {
      const e = this.checkedList[index];
      if (e.classesName == thisObj.classesName && e.weekNum == thisObj.weekNum) {
        this.checkedList.splice(index, 1);
      }
    }

    if (obj.obj.checked) {
      if (this.checkedList.length >= 2) {
        this.checkedList[0].obj.checked = false
        this.checkedList.splice(0, 1);
      }
      this.checkedList.push(thisObj);
    }


    console.log(obj.cell);
    console.log(obj.obj.checked);
  }
  /**
   * 交换课程
   */
  ChangeCourse() {
    let postEnt={
      Key:[]
    }
    this.checkedList.forEach(element => {
      postEnt.Key.push({
        k:element.classesName,
        v:element.weekNum.replace('-','')
      });
    }); 

    console.log(postEnt);
    this.httpHelper.Post("WeekCourseChange/ChangeWeekCourse", postEnt).then((result: DtoResultObj<string>) => {
      Fun.HideLoading();
      if (result.success) {
          this.source.refresh()
      }
      else {
        Fun.Hint(result.msg);
      }
      console.log(result);
    })
  }
}
